//
//  TaxInfoDetailViewController.m
//  Hxcsfw
//
//  Created by 企友软mac on 2017/12/25.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "TaxInfoDetailViewController.h"
#import "ZXCell.h"
#import "TaxInfoModel.h"
#import "WWebViewController.h"

@interface TaxInfoDetailViewController ()<UITableViewDelegate,UITableViewDataSource> {
    int j;
}

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arrTax;

@end

@implementation TaxInfoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrTax = [NSMutableArray array];
    self.title = @"政策微解读";
    j = 0;
    [self.view addSubview:self.tableView];
    [self.tableView.superview insertSubview:[UIView new] atIndex:0];
    [self requestListDataStrNum:@"1" isRefrsh:YES];
    [self notifyRequestListData];

}

- (void)noWifiRefershingEnd {
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
    [WShareUtil sharedData].isShowWait = YES;

}

- (void)notifyRequestListData{

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestListDataStrNum:@"1" isRefrsh:YES];
        j = 1;
    }];

    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSString *num = [NSString stringWithFormat:@"%d",++j];
        [self requestListDataStrNum:num isRefrsh:NO];
    }];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WSCREENH_WIDTH, WSCREENH_HEIGHT - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight=70.f;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.bounces = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrTax count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identCell = @"HomeListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identCell];

    if (cell == nil) {
        ZXCell *zXCell = [[[NSBundle mainBundle] loadNibNamed:@"ZXCell" owner:self options:nil] lastObject];
        if (_arrTax.count > 0 && _arrTax) {
            zXCell.lbTitle.text = [self.arrTax[indexPath.row] strTitle];

            [UILabel changeSpaceForLabel:zXCell.lbTitle];
            zXCell.lbNum.text = [NSString stringWithFormat:@"%@人听过",[self.arrTax[indexPath.row] strView]];
            zXCell.lbTime.text = [self.arrTax[indexPath.row] fbsj];
        }
        cell = zXCell;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WWebViewController *webVc = [[WWebViewController alloc] init];
    webVc.strTitle = @"政策微解读";
    
    webVc.shareTitle = [self.arrTax[indexPath.row] strTitle];//分享标题
    webVc.strID = [self.arrTax[indexPath.row] strId];//分享ID
    webVc.strType = @"sx";//分享类型税信
    
    webVc.strUrl = [self.arrTax[indexPath.row] link];
    [self.navigationController pushViewController:webVc animated:YES];
}

- (void)requestListDataStrNum:(NSString *)pageNum isRefrsh:(BOOL)isRefrsh{

    [WShareUtil sharedData].isShowWait = NO;

    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"xxfb",@"content":@{@"revert":@"paginate",@"colcode":@   "zcwjd",@"pagesize":@"10",@"pagenumber":pageNum}};

    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};


    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];

        if ([successStr isEqualToString:@"false"]) {
            return;
        }
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if (isRefrsh) {
            [self.arrTax removeAllObjects];
        }

        NSArray *arrData = returnDic[@"data"];

        for (NSDictionary *dic in arrData) {
            TaxInfoModel *taxInfoModel = [TaxInfoModel mj_objectWithKeyValues:dic];
            [self.arrTax addObject:taxInfoModel];
        }
        [self.tableView reloadData];
        [self noWifiRefershingEnd];
        
        ///* --- 图片式提醒
        UIView *view = [self.tableView viewWithTag:10000];
        [view removeFromSuperview];
        HHNonePage *_nonePage = [[HHNonePage alloc]init];
        _nonePage = nil;
        if (_arrTax.count == 0) {
            _nonePage = [[HHNonePage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            _nonePage.tag = 10000;
            [self.tableView addSubview:_nonePage];
        }
        //*/
        
        if (arrData.count < 10) {
            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
            [self.tableView.footer endRefreshingWithNoMoreData];
        }

    } failure:^(NSError *error) {
        [self noWifiRefershingEnd];
        ///* --- 图片式提醒
        UIView *view = [self.tableView viewWithTag:10000];
        [view removeFromSuperview];
        HHNonePage *_nonePage = [[HHNonePage alloc]init];
        _nonePage = nil;
        if (_arrTax.count == 0) {
            _nonePage = [[HHNonePage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            _nonePage.tag = 10000;
            [self.tableView addSubview:_nonePage];
        }
        //*/
        WLog(@"失败");
    } showProgress:YES];
}



@end
